package cn.fl.inscare.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.fl.capital.service.ICapPayInfoApiService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.inscare.service.IInsApplicationStagingService;
import cn.fl.inscare.transform.*;
import cn.fl.inscare.vo.*;
import cn.fl.insurance.condition.InsCompInfoQueryCondition;
import cn.fl.insurance.vo.InsCompInfoVO;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.dto.PrjPrdBscInfoDTO;
import cn.fl.project.dto.PrjPrdDtlDTO;
import cn.fl.project.dto.PrjPrdVehInfoDTO;
import cn.fl.project.dto.PrjQuatPrpsDTO;
import cn.fl.project.service.IProjectApiService;
import ins.car.facade.car.car.api.IInsCompInfoApiService;
import ins.car.facade.car.car.api.IInsQueryApiService;
import ins.car.facade.car.car.common.MoneyConvert;
import ins.car.facade.car.car.condition.*;
import ins.car.facade.car.car.constant.InsCompInfoRConstant;
import ins.car.facade.car.car.dto.*;
import ins.car.facade.car.prj.api.InsCarPayInfoService;
import ins.car.facade.car.prj.api.InsOrPrjCombinationApiService;
import ins.car.facade.car.prj.condition.InsPrjApplRQC;
import ins.car.facade.car.prj.dto.InsPrjApplRDTO;
import cn.fl.insurance.dto.InsCompInfoDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstBscInfoDTO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  分期申请
 * Created by qxl on
 * 2018/9/11
 */
@Service
public class InsApplicationStagingServiceImpl implements IInsApplicationStagingService {

    private static final Logger logger = LoggerFactory.getLogger(InsApplicationStagingServiceImpl.class);
    @Autowired
    private InsOrPrjCombinationApiService insOrPrjCombinationApiService;
    @Autowired
    private IProjectApiService projectApiService ;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;
    @Autowired
    private InsCarPayInfoService insCarPayInfoService;
    @Autowired
    private cn.fl.insurance.service.IInsCompInfoApiService insCompInfoApiService;
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IInsQueryApiService insQueryApiService;
    @Autowired
    private ICapPayInfoApiService capPayInfoApiService;
    @Override
    public List<InsApplicationVO> getInsQuoteInfoList(Long prjId,String paySchNo) throws BaseException {
        if (StringUtils.isEmpty(prjId) && StringUtils.isEmpty(paySchNo)){
            BaseLogUtils.info(logger, "getInsQuoteInfoList", "项目ID不能为空或者支付表ID不能都为空!", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取项目ID不能为空!");
        }
        //关联表和主表关联是否关联
        InsPrjApplRQC insPrjApplRQC = new InsPrjApplRQC();
        if (StringUtils.isNotEmpty(prjId)){
            insPrjApplRQC.setPrjId(prjId);
        }
        if (StringUtils.isNotEmpty(paySchNo)){
            insPrjApplRQC.setPaySchNo(paySchNo);
        }
        insPrjApplRQC.setIsDel(BaseConstant.IS_YESNO_NO);
        List<InsPrjApplRDTO> insPrjAppR = this.insOrPrjCombinationApiService.getInsPrjAppR(insPrjApplRQC);
        //封装每一个申请表
        List<Long> cstIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(insPrjAppR)){
            for (InsPrjApplRDTO applRDTO: insPrjAppR){
                cstIds.add(applRDTO.getCstId());
            }
        }
        List<InsApplicationDTO> insApplicationDTOList = this.insOrPrjCombinationApiService.getInsApplicationList(prjId,paySchNo);
        List<InsApplicationVO> insApplicationVOList = InsApplicationVOTransform.toVOList(insApplicationDTOList);
        if (CollectionUtils.isNotEmpty(insApplicationVOList)){
            this.getUser(insApplicationVOList,cstIds);
            this.setValueList(insApplicationVOList);
            return insApplicationVOList;
        }
        return new ArrayList<>();
    }

    private List<InsApplicationVO> getUser(List<InsApplicationVO> insApplicationVOS, List<Long> cstIds){
        if (CollectionUtils.isEmpty(insApplicationVOS) && CollectionUtils.isNotEmpty(cstIds)){
            BaseLogUtils.info(logger, "getUser", "用戶id!", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用戶id不能为空!");
        }
        try {
            for (int i = 0; i < insApplicationVOS.size(); i++) {
                BaseLogUtils.info(logger, "getUser", "getUser的投保人id" + insApplicationVOS.get(i).getPerId());
                //查询客户信息，并且设置
                for (int j = 0; j < cstIds.size(); j++) {
                    if (StringUtils.isNotEmpty(cstIds.get(j))) {
                        insApplicationVOS.get(j).setCstUserId(cstIds.get(j));
                    }
                }
            }
        }catch (BaseException e){
            BaseLogUtils.info(logger, "getUser", "获取客户信息异常");
            e.getMessage();
        }catch (Exception e){
            BaseLogUtils.info(logger, "getUser", "获取客户信息异常");
            e.getMessage();
        }
        return insApplicationVOS;
    }

    private void setInsValue(List<InsApplicationVO> insQuoteInfoVOS,InsApplicationVO user){
        if (null == user){
            return;
        }
        for (InsApplicationVO idAndType :insQuoteInfoVOS ){
            if (StringUtils.isNotEmpty(user.getInsPerType())){
                idAndType.setCstTypCd(user.getInsPerType());
            }
            if (StringUtils.isNotEmpty(user.getPerId())){
                idAndType.setCstUserId(user.getPerId());
            }
        }
    }


    //setValueList
    private void setValueList(List<InsApplicationVO> insApplicationVO){
        if (CollectionUtils.isEmpty(insApplicationVO)){
            return;
        }
        for (InsApplicationVO voList :insApplicationVO){
            //通过每一个applicationId去查找
            //保单中保险详情表
            InsPolicyDetailQC insPolicyDetailQC = new InsPolicyDetailQC();
            insPolicyDetailQC.setApplicationId(voList.getId());
            insPolicyDetailQC.setIsDel(BaseConstant.IS_YESNO_NO);
            List<InsPolicyDetailDTO> insPolicyDetailDTOS = this.insOrPrjCombinationApiService.getApplicationId(insPolicyDetailQC);
            List<InsPolicyDetailVO> insPolicyDetailVOS = InsPolicyDetailVOTransform.toVOList(insPolicyDetailDTOS);
            if (CollectionUtils.isNotEmpty(insPolicyDetailVOS)){
                //取商业险保费  交强险保费  商业险保险期限  交强险保险期限
                for (InsPolicyDetailVO vo:  insPolicyDetailVOS){
                    if (vo.getInsTypeCd().equals(1) || vo.getInsTypeCd() == 1){
                        if (StringUtils.isNotEmpty(String.valueOf(vo.getInsBeginDt())) && StringUtils.isNotEmpty(String.valueOf(vo.getInsEndDt()))){//开始时间
                            voList.setPlcBgTimeNm(DateUtils.formatDateToString(vo.getInsBeginDt(), "yyyy-MM-dd")+" 至 "+ DateUtils.formatDateToString(vo.getInsEndDt(), "yyyy-MM-dd"));
                        }
                        if (StringUtils.isNotEmpty(String.valueOf(vo.getInsEndDt()))){//结束时间
                            voList.setPlcEndTimeNm(DateUtils.formatDateToString(vo.getInsEndDt(), "yyyy-MM-dd"));
                        }
                        if (StringUtils.isNotEmpty(vo.getInsAmt())){//商业险保费
                            voList.setPlcPriceNm(DecimalUtils.convertFenToYuan(vo.getInsAmt()));
                        }

                    }else if(vo.getInsTypeCd().equals(2) || vo.getInsTypeCd() == 2){//交强险
                        if (StringUtils.isNotEmpty(String.valueOf(vo.getInsBeginDt())) && StringUtils.isNotEmpty(String.valueOf(vo.getInsEndDt()))){//开始时间
                            voList.setInsBgTmBsNm(DateUtils.formatDateToString(vo.getInsBeginDt(), "yyyy-MM-dd")+" 至 "+ DateUtils.formatDateToString(vo.getInsEndDt(), "yyyy-MM-dd"));
                        }
                        if (StringUtils.isNotEmpty(String.valueOf(vo.getInsEndDt()))){//结束时间
                            voList.setInsEndTmBsNm(DateUtils.formatDateToString(vo.getInsEndDt(), "yyyy-MM-dd"));
                        }
                        if (StringUtils.isNotEmpty(vo.getInsAmt())){//交强险保费
                            voList.setInsPriceNm(DecimalUtils.convertFenToYuan(vo.getInsAmt()));
                        }
                    }
                }
            }
        }
    }


    @Override
    public InsApplicationVO getApplicationId(Long applicationId, String cstNm) throws BaseException {
        if (StringUtils.isEmpty(String.valueOf(applicationId)) &&StringUtils.isEmpty(cstNm)){
            //BaseLogUtils.info(logger, "getApplicationId", "申请id和客户姓名不能为空!", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "申请id和客户姓名不能为空!");
        }
        InsApplicationVO insApplicationVO = new InsApplicationVO();
        //申请表id
        if(StringUtils.isNotEmpty(applicationId))//
            insApplicationVO= this.getCstNm(applicationId);
        return insApplicationVO;
    }

    //点客户 或者 申請表id
    private InsApplicationVO getCstNm(long applicationId ){
        InsApplicationQC qc = new InsApplicationQC();
        if (StringUtils.isNotEmpty(applicationId)){
            qc.setId(applicationId);
        }
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        //通过姓名得到主表信息
        InsApplicationDTO dto = this.insOrPrjCombinationApiService.getApplicationId(qc);
        InsApplicationVO insApplicationVO = InsApplicationVOTransform.toVO(dto);
        if (insApplicationVO != null){
            //获取到主表id
            Long id = insApplicationVO.getId();
            InsVehicleLicenseVO insVehicleLicenseVO = new InsVehicleLicenseVO();
            InsQuoteInfoVO insQuoteInfoVO = new InsQuoteInfoVO();
            List<InsPolicyDetailVO> insPolicyDetailVOS = new ArrayList<>();
            // InsVehicleLicenseVO insVehicleLicenseVO; InsQuoteInfoVO insQuoteInfoVO
            //################封装行驶证id
            List<Long> ids = new ArrayList<>();
            ids.add(insApplicationVO.getVehicleLicenseId());
            //################封装行驶证id
            //车辆信息
            InsVehicleLicenseQC insVehicleLicenseQC = new InsVehicleLicenseQC();
            insVehicleLicenseQC.setIds(ids);
            insVehicleLicenseQC.setIsDel(BaseConstant.IS_YESNO_NO);
            InsVehicleLicenseDTO insVehicleLicenseDTO =
                    this.insOrPrjCombinationApiService.getApplicationId(insVehicleLicenseQC);
            if (insVehicleLicenseDTO != null){
                insVehicleLicenseVO = InsVehicleLicenseVOTransform.toVO(insVehicleLicenseDTO);
            }

            //保险信息
            InsQuoteInfoQC insQuoteInfoQC = new InsQuoteInfoQC();
            insQuoteInfoQC.setApplicationId(id);
            insQuoteInfoQC.setIsDel(BaseConstant.IS_YESNO_NO);
            InsQuoteInfoDTO insQuoteInfoDTO = this.insOrPrjCombinationApiService.getApplicationId(insQuoteInfoQC);
            if (insQuoteInfoDTO != null){
                insQuoteInfoVO = InsQuoteInfoVOTransform.toVO(insQuoteInfoDTO);
            }
            //保单中保险详情表
            InsPolicyDetailQC insPolicyDetailQC = new InsPolicyDetailQC();
            insPolicyDetailQC.setApplicationId(id);
            insPolicyDetailQC.setIsDel(BaseConstant.IS_YESNO_NO);
            List<InsPolicyDetailDTO> insPolicyDetailDTO = this.insOrPrjCombinationApiService.getApplicationId(insPolicyDetailQC);
            if(CollectionUtils.isNotEmpty(insPolicyDetailDTO)){
                insPolicyDetailVOS = InsPolicyDetailVOTransform.toVOList(insPolicyDetailDTO);
                //setinsPolicyDetailVOS
                this.setinsPolicyDetailVOS(insPolicyDetailVOS,insApplicationVO);
            }
            //封装图片
            //车辆封装
            insApplicationVO.setInsVehicleLicenseVO(insVehicleLicenseVO);
            //保险封装
            insApplicationVO.setInsQuoteInfoVO(insQuoteInfoVO);
        }
        return insApplicationVO;
    }

    private void setinsPolicyDetailVOS(List<InsPolicyDetailVO> insPolicyDetailVOList,InsApplicationVO insApplicationVO){
        if (CollectionUtils.isEmpty(insPolicyDetailVOList)){
            return;
        }
        for (InsPolicyDetailVO volist:  insPolicyDetailVOList){
            Map<String, Object> insPolilyFile = this.insOrPrjCombinationApiService.getInsPolilyFile(volist.getApplicationId()); //getInsFile(String.valueOf(volist.getApplicationId()),  cfgCdb ,  orgCd ,   sysCd);
            //商业险开始时间
            //商业险结束时间
            //商业险投保单号
            //商业险保费
            //商业险投保单上传
            //商业险
            if (volist.getInsTypeCd().equals(1) || volist.getInsTypeCd() == 1){
                if (StringUtils.isNotEmpty(String.valueOf(volist.getInsBeginDt()))){//开始时间
                    insApplicationVO.setPlcBgTimeNm(DateUtils.formatDateToString(volist.getInsBeginDt(), "yyyy-MM-dd"));
                }
                if (StringUtils.isNotEmpty(String.valueOf(volist.getInsEndDt()))){//结束时间
                    insApplicationVO.setPlcEndTimeNm(DateUtils.formatDateToString(volist.getInsEndDt(), "yyyy-MM-dd"));
                }
                if (StringUtils.isNotEmpty(volist.getInsAmt())){//保费
                    insApplicationVO.setPlcPriceNm(DecimalUtils.convertFenToYuan(volist.getInsAmt()));
                }
                if (StringUtils.isNotEmpty(volist.getPlcNo())){//保单号
                    insApplicationVO.setPlcNumber(volist.getPlcNo());
                }
            }else if(volist.getInsTypeCd().equals(2) || volist.getInsTypeCd() == 2){//交强险
                if (StringUtils.isNotEmpty(String.valueOf(volist.getInsBeginDt()))){//开始时间
                    insApplicationVO.setInsBgTmBsNm(DateUtils.formatDateToString(volist.getInsBeginDt(), "yyyy-MM-dd"));
                }
                if (StringUtils.isNotEmpty(String.valueOf(volist.getInsEndDt()))){//结束时间
                    insApplicationVO.setInsEndTmBsNm(DateUtils.formatDateToString(volist.getInsEndDt(), "yyyy-MM-dd"));
                }
                if (StringUtils.isNotEmpty(volist.getInsAmt())){//保费
                    insApplicationVO.setInsPriceNm(DecimalUtils.convertFenToYuan(volist.getInsAmt()));
                }
                if (StringUtils.isNotEmpty(volist.getPlcNo())){//保单号
                    insApplicationVO.setInsNumber(volist.getPlcNo());
                }
            }
        }
    }




    @Override
    public QuotationVO getApplicationId(Long applicationId) throws BaseException {
        if(StringUtils.isEmpty(applicationId)){
            BaseLogUtils.info(logger, "getApplicationId", "申请ID不能为空!", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "申请ID不能为空!");
        }
        QuotationVO quotationVO = new QuotationVO();
/*        InsPrjApplRQC qc = new InsPrjApplRQC();
        qc.setPrjId(prjId);
        InsPrjApplRDTO insPrjAppR = this.insOrPrjCombinationApiService.getInsPrjAppR(qc);//获取到applicationId*/
        //申请表id
        if (StringUtils.isNotEmpty(applicationId)) {
            //录入版测算表
            InsLogComputeQC insLogComputeQC = new InsLogComputeQC();
            insLogComputeQC.setApplicationId(applicationId);
            InsLogComputeDTO insLogCompute = this.insOrPrjCombinationApiService.getInsLogCompute(insLogComputeQC);
            if (insLogCompute != null){
                InsLogComputeVO insLogComputeVO = InsLogComputeVOTransform.toVO(insLogCompute);
                this.steInsLogComputeValue(insLogComputeVO);
                quotationVO.setInsLogComputeVO(insLogComputeVO);
            }
            //客户还款详情表_测算
            InsQuoteComputeQC insQuoteComputeQC = new InsQuoteComputeQC();
            insQuoteComputeQC.setApplicationId(applicationId);
            InsQuoteComputeDTO insQuoteCompute = this.insOrPrjCombinationApiService.getInsQuoteCompute(insQuoteComputeQC);
            if (insLogCompute != null){
                InsQuoteComputeVO insQuoteComputeVO = InsQuoteComputeVOTransform.toVO(insQuoteCompute);
                this.setInsQuoteComputeValue(insQuoteComputeVO);
                quotationVO.setInsQuoteComputeVO(insQuoteComputeVO);
            }
            //客户报价版测算表
        }
        return quotationVO;
    }
    //設置InsLogComputeVO
    private void steInsLogComputeValue(InsLogComputeVO insLogCompVO){
        if (null != insLogCompVO){
            //保险金额
            insLogCompVO.setInsAmtNm(MoneyConvert.fen2Yuan(insLogCompVO.getInsAmt()).doubleValue());
            //融资额
            insLogCompVO.setFinancingAmtNm(MoneyConvert.fen2Yuan(insLogCompVO.getFinancingAmt()).doubleValue());
            //首期租金
            insLogCompVO.setFirstRentAmtNm(MoneyConvert.fen2Yuan(insLogCompVO.getFirstRentAmt()).doubleValue());
            //咨询费
            insLogCompVO.setConsultingFeeAmtNm(MoneyConvert.fen2Yuan(insLogCompVO.getConsultingFeeAmt()).doubleValue());
            //保证金金额
            insLogCompVO.setDepositAmtNm(MoneyConvert.fen2Yuan(insLogCompVO.getDepositAmt()).doubleValue());
            //留购价
            insLogCompVO.setPurchasePriceNm(MoneyConvert.fen2Yuan(insLogCompVO.getPurchasePrice()).doubleValue());
            //首付款合计
            insLogCompVO.setFirstAmtToltal(DecimalUtils.convertFenToYuan(insLogCompVO.getFirstAmt()));
            //手机号
            if(null != insLogCompVO.getApplicationId()){
                insLogCompVO.setTel(String.valueOf(insLogCompVO.getApplicationId()).substring(4,15));
            }
            //期限
            insLogCompVO.setPayPd(insLogCompVO.getPayPd());

            //首期租金比例
            insLogCompVO.setFirstRentalPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getFirstRentalPct())).doubleValue());
            //咨询费比例
            insLogCompVO.setConsultingFeePct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getConsultingFeePct())).doubleValue());
            //保证金比例
            insLogCompVO.setDepositPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getDepositPct())).doubleValue());
            //月平均利息
            insLogCompVO.setAvgMonInterest(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getAvgMonInterest())).doubleValue());
            //年平均利息
            insLogCompVO.setAnnualAvgInterest(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getAnnualAvgInterest())).doubleValue());
            //年利率
            insLogCompVO.setAnnualRate(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getAnnualRate())).doubleValue());
            //IRR
            insLogCompVO.setIrrPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(insLogCompVO.getIrrPct())).doubleValue());
        }
    }


    //設置InsQu
    private void setInsQuoteComputeValue(InsQuoteComputeVO vo){
        if (null != vo){
            //调整比例
            vo.setAdjustPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getAdjustPct())).doubleValue());
            //年平均利息
            vo.setAnnualAvgInterest(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getAnnualAvgInterest())).doubleValue());
            //年利率
            vo.setAnnualRate(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getAnnualRate())).doubleValue());
            //月平均利息
            vo.setAvgMonInterest(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getAvgMonInterest())).doubleValue());
            //咨询费金额
            vo.setConsultingFeeAmtNm(DecimalUtils.convertFenToYuan(vo.getConsultingFeeAmt()));
            //咨询费比例
            vo.setConsultingFeePct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getConsultingFeePct())).doubleValue());
            //保证金金额
            vo.setDepositAmtNm(DecimalUtils.convertFenToYuan(vo.getDepositAmt()));
            //保证金比例
            vo.setDepositPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getDepositPct())).doubleValue());
            //融资额
            vo.setFinancingAmtNm(DecimalUtils.convertFenToYuan(vo.getFinancingAmt()));
            //首付金额
            vo.setFirstAmtNm(DecimalUtils.convertFenToYuan(vo.getFirstAmt()));
            //首期租金比例
            vo.setFirstRentalPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getFirstRentalPct())).doubleValue());
            //首期租金
            vo.setFirstRentAmtNm(DecimalUtils.convertFenToYuan(vo.getFirstRentAmt()));
            //保险金额
            vo.setInsAmtNm(DecimalUtils.convertFenToYuan(vo.getInsAmt()));
            //留购价
            vo.setPurchasePriceNm(DecimalUtils.convertFenToYuan(vo.getPurchasePrice()));
            //irr比例
            vo.setIrrPct(MoneyConvert.setMultiplyScale(BigDecimal.valueOf(vo.getIrrPct())).doubleValue());
        }
    }

    @Override
    public Map<String, Object> getInsPolilyFile(Long id) {
        return this.insOrPrjCombinationApiService.getInsPolilyFile(id);
    }

   /* private InsApplicationVO setValue(){

    }*/

    @Override
    public Map<String,Object> getApplicationR(Long prjPrdDtlId) {
        InsApplicationDTO dto = insOrPrjCombinationApiService.getInsApplication(prjPrdDtlId);
        if(dto == null){
            return null;
        }
        Long applyId  = dto.getId();
        List<InsPolicyDetailDTO> polic = insOrPrjCombinationApiService.getPolicyDetail(applyId);
        Map<String,Object> map = new HashMap<>();
        map.put("application", dto);
        map.put("polic", polic);
        return map;
    }

    @Override
    public String getInsApplication(AddAplicationVo addvo) {
        //1.产品详情Id查询 车辆信息,投保人,投保人id,支付表编号,支付表号,项目id,项目产品id
        //查询产品明细
        PrjPrdDtlDTO prjdto = projectApiService.selectPrjPrdDtlById(Long.valueOf(addvo.getPrjPrdDtlId()));
        BaseLogUtils.info(logger, "getInsApplication", "产品明细查询",JSONObject.toJSONString(prjdto));
        if(prjdto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "产品明细查询为空");
        }
        Long  actprdid= prjdto.getActPrdId();//车辆实际ID
        Long  prjid = prjdto.getPrjId();//项目ID
        BaseLogUtils.info(logger, "getInsApplication", "车辆实际ID",JSONObject.toJSONString(actprdid));
        if(actprdid == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "车辆实际ID为空");
        }
        BaseLogUtils.info(logger, "getInsApplication", "项目ID",JSONObject.toJSONString(prjid));
        if(prjid == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "项目ID为空");
        }

        //查找项目信息
        PrjBscInfoDTO prjbac = projectApiService.selectPrjBscInfoById(prjid, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if(prjbac == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "项目信息查询为空");
        }
        Long cstid = prjbac.getCstId();//客户id
        BaseLogUtils.info(logger, "getInsApplication", "项目信息",JSONObject.toJSONString(prjbac));
        //查询车辆信息
        PrjPrdVehInfoDTO prjVehDto = projectApiService.selectPrjPrdVehInfoById(actprdid,CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        BaseLogUtils.info(logger, "getInsApplication", "客户id",JSONObject.toJSONString(prjVehDto));
        if(prjVehDto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询车辆信息为空");
        }
        BaseLogUtils.info(logger, "getInsApplication", "客户id",JSONObject.toJSONString(cstid));
        if(cstid == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "客户id为空");
        }
        //查询客户基本信息
        CstBscInfoDTO cstDto = customerQueryApiService.getCstBscInfoById(cstid);
        if(cstDto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询客户基本信息为空");
        }
        BaseLogUtils.info(logger, "getInsApplication", "查询客户基本信息",JSONObject.toJSONString(cstDto));
        PrjQuatPrpsDTO prjDto =projectApiService.offerInformation(prjid);
        if(prjDto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询结果为空");
        }
        //根据项目产品ID查询品牌信息
        PrjPrdBscInfoDTO prjPrdBscInfoDTO = projectApiService.selectPrjPrdBscInfoById(prjdto.getPrjPrdId(), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if(prjPrdBscInfoDTO == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询结果为空");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("prjPrdDtlId", Long.valueOf(addvo.getPrjPrdDtlId()));//产品明细id
        map.put("prjPrdId", prjdto.getPrjPrdId());//产品id
        map.put("prjid", prjid);//项目id
        map.put("cstid", cstid);//客户id
        map.put("paySchId", prjdto.getPaySchId());//支付表id
        map.put("paySchNo", prjdto.getPaySchNo());//支付表编号
        map.put("annIntRatePct", prjDto.getAnnIntRatePct()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getAnnIntRatePct()).divide(new BigDecimal(100)));// 租赁年利率
        map.put("firstYearRate", prjDto.getFirstYearRate()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getFirstYearRate()).divide(new BigDecimal(100)));// prjDto.getFirstYearRate()/100);//首期租金比例
        map.put("customerDepositRate", prjDto.getCustomerDepositRate()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getCustomerDepositRate()).divide(new BigDecimal(100)));// prjDto.getCustomerDepositRate()/100);//保证金比例值
        //2.将查询到的结果填到Dto上
        InsVehicleLicenseDTO vehDto = new InsVehicleLicenseDTO();
        vehDto.setVin(prjVehDto.getVin());//车架号
        vehDto.setEngineNo(prjVehDto.getEngNo());//发动机号
      //  String model="";
/*        String VehBr =  prjVehDto.getVehBr();
        String PrdMdl = prjVehDto.getPrdMdl();
        model += StringUtils.isEmpty(VehBr) == true ? "" : VehBr;
        model += StringUtils.isEmpty(PrdMdl) == true ? "" : PrdMdl;*/
        String VehBr = prjPrdBscInfoDTO.getBrCd();//品牌
        String PrdMdl = prjPrdBscInfoDTO.getPrdMdl();//产品型号
        vehDto.setModel(VehBr+PrdMdl);//产品型号
        vehDto.setState("1");
        vehDto.setOwner(cstDto.getCstNm());
        if(prjVehDto.getCertDt() != null){
            vehDto.setIssueDt(DateUtils.formatDateToString(prjVehDto.getCertDt(), "yyyy-MM-dd"));
        }
        if(prjVehDto.getProDt() != null){
            vehDto.setRegDt(DateUtils.formatDateToString(prjVehDto.getProDt(), "yyyy-MM-dd"));

        }
        vehDto.setUsrId(cstDto.getUsrId());//用户id
        vehDto.setUsrNm(cstDto.getCstNm());//用户姓名
        vehDto.setUsrMp(cstDto.getMp1());//手机号

        InsApplicationDTO appDto = new InsApplicationDTO();
        appDto.setId(Long.valueOf(addvo.getId()));
        appDto.setPerNm(cstDto.getCstNm());//投保人
        appDto.setPerId(cstDto.getUsrId());//投保人ID
        appDto.setInsPerType(addvo.getCertTypCd());//被保险人类型
        appDto.setInsPerNm(addvo.getInsPerNm());//被保险人
        appDto.setCertNm(addvo.getInsPerNm());//被保险人

        appDto.setCertNo(addvo.getIdCard());//被保险人证件号码
        appDto.setInsPerMobile(addvo.getMp());//被保人手机号
        appDto.setInsCompId(Long.valueOf(addvo.getInsCompCd()));//保险公司CD
        appDto.setInsCompNm(addvo.getInsCompNm());//保险公司
        appDto.setInsCompSubId(Long.valueOf(addvo.getInsCompSubCd()));//保险支公司CD
        appDto.setInsCompSubNm(addvo.getInsCompSubNm());//保险支公司
        appDto.setRgnCyCd(addvo.getRgnCyCd());
        appDto.setRgnCyNm(addvo.getRgnCyNm());
        appDto.setRgnPrCd(addvo.getRgnPrCd());
        appDto.setRgnPrNm(addvo.getRgnPrNm());
        appDto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        appDto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());


        Long total = addvo.getInsPolicyJqAmt()+addvo.getInsPolicyBusAmt();
        appDto.setInsTotalAmt(total);//保费总额
        List<InsPolicyDetailDTO> list = new ArrayList<>();
        InsPolicyDetailDTO insPolicyDet = new  InsPolicyDetailDTO();
        insPolicyDet.setInsTypNm("商业险");
        insPolicyDet.setInsTypeCd(1L);
        insPolicyDet.setInsAmt(addvo.getInsPolicyBusAmt());
        insPolicyDet.setInsDiscountAmt(addvo.getInsPolicyBusAmt());
        if(addvo.getInsPolicyBusAmt()>0){

            insPolicyDet.setIsBuy("1");
        }else{
            insPolicyDet.setIsBuy("0");
        }
        DateUtils.formatStringToDate(addvo.getPlcBgTmBs(), DateUtils.YYYY_MM_DD);
        insPolicyDet.setInsBeginDt(DateUtils.formatStringToDate(addvo.getPlcBgTmBs(), DateUtils.YYYY_MM_DD));
        insPolicyDet.setInsEndDt(DateUtils.formatStringToDate(addvo.getPlcEndTmBs(), DateUtils.YYYY_MM_DD));
        InsPolicyDetailDTO insPolicyJq = new  InsPolicyDetailDTO();
        insPolicyJq.setInsTypNm("交强险");
        insPolicyJq.setInsTypeCd(2L);
        if(addvo.getInsPolicyJqAmt()>0){

            insPolicyJq.setIsBuy("1");
        }else{
            insPolicyJq.setIsBuy("0");
        }
        insPolicyJq.setInsAmt(addvo.getInsPolicyJqAmt());
        insPolicyJq.setInsDiscountAmt(addvo.getInsPolicyJqAmt());
        insPolicyJq.setInsBeginDt(DateUtils.formatStringToDate(addvo.getPlcBgTmJq(), DateUtils.YYYY_MM_DD));
        insPolicyJq.setInsEndDt(DateUtils.formatStringToDate(addvo.getPlcEndTmJq(), DateUtils.YYYY_MM_DD));
        list.add(insPolicyDet);
        list.add(insPolicyJq);
        Long b = insOrPrjCombinationApiService.addInsApplication(vehDto,appDto,list,map);
        try {
			if(StringUtils.isNotEmpty(b)){
			    CalculateDTO calDto = new  CalculateDTO();
			    calDto.setApplyId(appDto.getId());
			    calDto.setYearInterest(new BigDecimal(map.get("annIntRatePct").toString()));//年利率
			    calDto.setDepositPct(new BigDecimal(map.get("customerDepositRate").toString()));//保证金
			    calDto.setFirstRentalPct(new BigDecimal(map.get("firstYearRate").toString()));//首期比例
			    String success = insCarPayInfoService.addUsrPayInfoDetail(calDto);
			    if(StringUtils.isEmpty(success)){//成功
			    	boolean result= capPayInfoApiService.modifyPayAmt(prjdto.getPaySchId());
			    	if(result){
			    		return "1";
			    	}
			    	this.deleteApplication(Long.valueOf(addvo.getId()));
			    	return "0";
			    }else {//生成支付表示失败
			    	BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",JSONObject.toJSONString(success));
			    	this.deleteApplication(Long.valueOf(addvo.getId()));
			    	return success;
				}
			}else{//失败
			    return "0";
			}
		}catch (BaseException e) {
	    	BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",e.getMessage());
	    	this.deleteApplication(Long.valueOf(addvo.getId()));
	    	return e.getMessage();
		}catch (Exception ex) {
	    	BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",ex.getMessage());
	    	this.deleteApplication(Long.valueOf(addvo.getId()));
	    	return ex.getMessage();
		} 
    }

    @Override
    public String modifyApplication(AddAplicationVo addvo, Long applyId) {
        PrjPrdDtlDTO prjdto = projectApiService.selectPrjPrdDtlById(Long.valueOf(addvo.getPrjPrdDtlId()));
        BaseLogUtils.info(logger, "getInsApplication", "产品明细查询",JSONObject.toJSONString(prjdto));
        if(prjdto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "产品明细查询为空");
        }

        InsApplicationDTO appDto = new InsApplicationDTO();
        appDto.setId(applyId);
        appDto.setInsPerType(addvo.getCertTypCd());//被保险人类型
        if("1".equals(addvo.getCertTypCd())){
            appDto.setCertTypCd("0");
        }else{
            appDto.setCertTypCd("b");
        }
        appDto.setInsPerNm(addvo.getInsPerNm());//被保险人
        appDto.setCertNm(addvo.getInsPerNm());//被保险人
        appDto.setCertNo(addvo.getIdCard());//被保险人证件号码
        appDto.setInsPerMobile(addvo.getMp());//被保人手机号
        appDto.setInsCompId(Long.valueOf(addvo.getInsCompCd()));//保险公司CD
        appDto.setInsCompNm(addvo.getInsCompNm());//保险公司
        appDto.setInsCompSubId(Long.valueOf(addvo.getInsCompSubCd()));//保险支公司CD
        appDto.setInsCompSubNm(addvo.getInsCompSubNm());//保险支公司
        appDto.setRgnCyCd(addvo.getRgnCyCd());
        appDto.setRgnCyNm(addvo.getRgnCyNm());
        appDto.setRgnPrCd(addvo.getRgnPrCd());
        appDto.setRgnPrNm(addvo.getRgnPrNm());
        appDto.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        Long total = addvo.getInsPolicyJqAmt()+addvo.getInsPolicyBusAmt();
        appDto.setInsTotalAmt(total);//保费总额

        List<InsPolicyDetailDTO> list = new ArrayList<>();
        InsPolicyDetailDTO insPolicyDet = new  InsPolicyDetailDTO();
        insPolicyDet.setInsTypNm("商业险");
        insPolicyDet.setInsTypeCd(1L);
        insPolicyDet.setInsAmt(addvo.getInsPolicyBusAmt());
        insPolicyDet.setInsDiscountAmt(addvo.getInsPolicyBusAmt());
        if(addvo.getInsPolicyBusAmt()>0){

            insPolicyDet.setIsBuy("1");
        }else{
            insPolicyDet.setIsBuy("0");
        }
        DateUtils.formatStringToDate(addvo.getPlcBgTmBs(), DateUtils.YYYY_MM_DD);
        insPolicyDet.setInsBeginDt(DateUtils.formatStringToDate(addvo.getPlcBgTmBs(), DateUtils.YYYY_MM_DD));
        insPolicyDet.setInsEndDt(DateUtils.formatStringToDate(addvo.getPlcEndTmBs(), DateUtils.YYYY_MM_DD));
        InsPolicyDetailDTO insPolicyJq = new  InsPolicyDetailDTO();
        insPolicyJq.setInsTypNm("交强险");
        insPolicyJq.setInsTypeCd(2L);
        if(addvo.getInsPolicyJqAmt()>0){

            insPolicyJq.setIsBuy("1");
        }else{
            insPolicyJq.setIsBuy("0");
        }
        insPolicyJq.setInsAmt(addvo.getInsPolicyJqAmt());
        insPolicyJq.setInsDiscountAmt(addvo.getInsPolicyJqAmt());
        if(StringUtils.isEmpty(addvo.getPlcBgTmJq())){
        	 insPolicyJq.setInsBeginDt(null);
             insPolicyJq.setInsEndDt(null);
        }else{
        	insPolicyJq.setInsBeginDt(DateUtils.formatStringToDate(addvo.getPlcBgTmJq(), DateUtils.YYYY_MM_DD));
        	insPolicyJq.setInsEndDt(DateUtils.formatStringToDate(addvo.getPlcEndTmJq(), DateUtils.YYYY_MM_DD));
        }
        list.add(insPolicyDet);
        list.add(insPolicyJq);
        Long  prjid = prjdto.getPrjId();//项目ID
        if(prjid == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "项目ID为空");
        }
        PrjQuatPrpsDTO prjDto =projectApiService.offerInformation(prjid);
        if(prjDto == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询结果为空");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("annIntRatePct", prjDto.getAnnIntRatePct()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getAnnIntRatePct()).divide(new BigDecimal(100)));// 租赁年利率
        map.put("firstYearRate", prjDto.getFirstYearRate()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getFirstYearRate()).divide(new BigDecimal(100)));// prjDto.getFirstYearRate()/100);//首期租金比例
        map.put("customerDepositRate", prjDto.getCustomerDepositRate()== null ? new BigDecimal(0) : new BigDecimal(prjDto.getCustomerDepositRate()).divide(new BigDecimal(100)));// prjDto.getCustomerDepositRate()/100);//保证金比例值
        boolean b =  insOrPrjCombinationApiService.modifyApplication(appDto,list,applyId);
        try {
			if(b){
			    CalculateDTO calDto = new  CalculateDTO();
			    calDto.setApplyId(appDto.getId());
			    calDto.setYearInterest(new BigDecimal(map.get("annIntRatePct").toString()));//年利率
			    calDto.setDepositPct(new BigDecimal(map.get("customerDepositRate").toString()));//保证金
			    calDto.setFirstRentalPct(new BigDecimal(map.get("firstYearRate").toString()));//首期比例
			    String success = insCarPayInfoService.addUsrPayInfoDetail(calDto);
			    if(StringUtils.isEmpty(success) ){
			    	boolean result= capPayInfoApiService.modifyPayAmt(prjdto.getPaySchId());
			    	if(result){
			    		return "1";
			    	}
			    	this.deleteApplication(applyId);
			    	return "0";
			    }else{
			    	BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",JSONObject.toJSONString(success));
			    	this.deleteApplication(applyId);
			    	return success;
			    }
			}else{
				return "0";
			}
		}catch (BaseException e) {
	    	BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",e.getMessage());
	    	this.deleteApplication(Long.valueOf(addvo.getId()));
	    	return e.getMessage();
		}catch (Exception ex) {
			BaseLogUtils.info(logger, "getInsApplication", "生成支付表失败",ex.getMessage());
	    	this.deleteApplication(Long.valueOf(addvo.getId()));
	    	return ex.getMessage();
		}
    }

    @Override
    public Pager  getInsApplication(Pager page,Long prjId) {
    	Pager pager = insOrPrjCombinationApiService.getInsApplicationList(page ,prjId);
    	List<InsApplicationDTO> r = (List<InsApplicationDTO>)pager.getResultList();
    	List<Long> vehId= new ArrayList<>();
    	List<Long> applyId= new ArrayList<>();
    	for (InsApplicationDTO insApplicationDTO : r) {
    		vehId.add(insApplicationDTO.getVehicleLicenseId());
    		applyId.add(insApplicationDTO.getId());
		}
    	//查询车辆信息
    	List<InsVehicleLicenseDTO> VehicleList = insOrPrjCombinationApiService.getVehicleList(vehId);
    	HashMap<Long, InsVehicleLicenseDTO> hashMap = new HashMap<>();
    	for (InsVehicleLicenseDTO insVehicleLicenseDTO : VehicleList) {
    		hashMap.put(insVehicleLicenseDTO.getId(), insVehicleLicenseDTO);
		}
    	//查询
    	List<InsPolicyDetailDTO> policyDetailList = insOrPrjCombinationApiService.getPolicyDetailList(applyId);
    	HashMap<String, InsPolicyDetailDTO> map = new HashMap<String, InsPolicyDetailDTO>();
    	for (InsPolicyDetailDTO insPolicyDetailDTO : policyDetailList) {
    		map.put(insPolicyDetailDTO.getApplicationId()+"@"+insPolicyDetailDTO.getInsTypeCd(), insPolicyDetailDTO);
		}
    	for (InsApplicationDTO appDto : r) {
    		if(hashMap.containsKey(appDto.getVehicleLicenseId())){
    			appDto.setVin(hashMap.get(appDto.getVehicleLicenseId()).getVin());
    			appDto.setPlateNo(hashMap.get(appDto.getVehicleLicenseId()).getPlateNo());
    			appDto.setEngineNo(hashMap.get(appDto.getVehicleLicenseId()).getEngineNo());
    			appDto.setVehicleType(hashMap.get(appDto.getVehicleLicenseId()).getModel().replace("null", ""));
    		}
    		if(map.containsKey(appDto.getId()+"@1")){//商业险
    			String buy = map.get(appDto.getId()+"@1").getIsBuy();
    			if("1".equals(buy)){
    				appDto.setBusAmt(MoneyConvert.fen2Yuan(map.get(appDto.getId()+"@1").getInsDiscountAmt()));
    			}else{
    				appDto.setBusAmt(new BigDecimal(0));
    			}
    		}
    		if(map.containsKey(appDto.getId()+"@2")){//交强险
    			String buy = map.get(appDto.getId()+"@2").getIsBuy();
    			if("1".equals(buy)){
    				appDto.setJqAmt(MoneyConvert.fen2Yuan(map.get(appDto.getId()+"@2").getInsDiscountAmt()));
    			}else{
    				appDto.setJqAmt(new BigDecimal(0));
    			}
    		}
    		
    	}
    	
        return pager;
    }

    @Override
    public boolean deleteApplication(Long applyId) throws BaseException {
        if(applyId == null){
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "applyId为空");
        }

        return insOrPrjCombinationApiService.deleteApplication(applyId);
    }

    @Override
    public Long checkProjectInformation(Long prjId ,Long type) throws BaseException {

        return projectApiService.checkProjectInformation(prjId,type);
    }

    @Override
    public Long searchApplicationId() throws BaseException {

        return insOrPrjCombinationApiService.searchApplicationId();
    }

    @Override
    public boolean checkData(Long prjPrdDtlId) throws BaseException {

        return insOrPrjCombinationApiService.checkData(prjPrdDtlId);
    }

	@Override
	public boolean checkVehon(Long prjPrdDtlId) throws BaseException {
		//查询产品明细
		PrjPrdDtlDTO prjdto = projectApiService.selectPrjPrdDtlById(prjPrdDtlId);
		BaseLogUtils.info(logger, "checkVehon", "产品明细查询",JSONObject.toJSONString(prjdto));
		if(prjdto == null){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "产品明细查询为空");
		}
		Long  actprdid= prjdto.getActPrdId();//车辆实际ID
		if(StringUtils.isEmpty(actprdid)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "合格证信息不能为空");
		}
		//查询合格证信息
		PrjPrdVehInfoDTO prjVehDto = projectApiService.selectPrjPrdVehInfoById(actprdid,CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		BaseLogUtils.info(logger, "checkVehon", "合格证信息",JSONObject.toJSONString(prjVehDto));
		if(prjVehDto == null || StringUtils.isEmpty(prjVehDto.getCertNo())){
			return false;
		}
		return true;
	}
	
	@Override
	public InsApplicationDTO searchAppIdById(Long applyId) throws BaseException {
		
		return insOrPrjCombinationApiService.getApplicationId(applyId);
	}

	@Override
	public Map<String, Object> getApplication(Long applyId)throws BaseException {
		    InsPrjApplRDTO insPrjApplRDTO = insOrPrjCombinationApiService.getPrjprdDtl(applyId);
		    if(insPrjApplRDTO == null){
		    	throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "查询结果为空");
		    }
		 	InsApplicationDTO dto = insOrPrjCombinationApiService.getApplicationId(applyId);
	        if(dto == null){
	            return null;
	        }
	        Long id  = dto.getId();
	        List<InsPolicyDetailDTO> polic = insOrPrjCombinationApiService.getPolicyDetail(id);
	        Map<String,Object> map = new HashMap<>();
	        map.put("application", dto);
	        map.put("polic", polic);
	        map.put("prjPrdDtlId", insPrjApplRDTO.getPrjPrdDtlId());
	        return map;
	}

	@Override
	public InsPrjApplRDTO getPrjPplR(Long applyId) throws BaseException {
		
		return insOrPrjCombinationApiService.getPrjprdDtl(applyId);
	}

	@Override
	public InsApplicationVO getInstallmentInformation(Long prjPrdDtlId, Long paySchId) throws BaseException {
		if (prjPrdDtlId == null && paySchId == null) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "prjPrdDtlId,paySchId为空");
		}
		InsApplicationVO vo = new InsApplicationVO();
		InsApplicationDTO dto = this.insOrPrjCombinationApiService.getCompInfo(prjPrdDtlId, paySchId);
		InsCompInfoQueryCondition condition = new InsCompInfoQueryCondition();
		condition.setId(dto.getInsCompSubId());
		List<InsCompInfoDTO> comp = this.insCompInfoApiService.searchInsCompInfoList(condition);
		if (CollectionUtils.isNotEmpty(comp)) {
			for (InsCompInfoDTO insCompInfoDTO : comp) {
				Map<String, String> feeTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "CST0014");
				if (feeTypCdMap != null && feeTypCdMap.containsKey(insCompInfoDTO.getBkId())) {
					vo.setBkNm(feeTypCdMap.get(insCompInfoDTO.getBkId()));//开户行名称
		    	}
				vo.setCompNm(insCompInfoDTO.getCompPidNm());//收款公司名称
				vo.setBkNo(insCompInfoDTO.getBkNo());//开户行号
				vo.setAccNo(insCompInfoDTO.getAccNo());//收款账号
			}
		} else {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询结果为空!");
		}
		return vo;
	}

    @Override
    public  List<KeyValueVO> selectSubInsCompList(int compLvl, InsCompInfoQC compQc) {
        InsCompInfoRQC qc = new InsCompInfoRQC();
        qc.setCompType(InsCompInfoRConstant.INS_COMP_TYPE_FQ);
        qc.setCompState(InsCompInfoRConstant.INS_COMP_STATE_CD);
        List<InsCompInfoRDTO> dtoList = null;
        List<KeyValueVO> provinceKVList = new ArrayList<KeyValueVO>();
        //保险分公司
        if (2 == compLvl && StringUtils.isNotEmpty(compQc.getCompPid())) {
            BaseLogUtils.info(logger, "getCompInfoList", "获取保险公司支公司信息");
            if(StringUtils.isEmpty(compQc.getCompPid())){
                BaseLogUtils.info(logger, "getCompInfoList", "保险公司ID为空");
                return null;
            }
            qc.setCompPid(compQc.getCompPid());
            qc.setRgnPrCd(compQc.getProCd());
            qc.setRgnCyCd(compQc.getCityCd());
            dtoList = this.insQueryApiService.selectSubInsCompList(qc);
        } else if(2 == compLvl && StringUtils.isEmpty(compQc.getCompPid())) {
            BaseLogUtils.info(logger, "getCompInfoList", "获取保险公司总公司信息");
            qc.setCompId(compQc.getCompId());
            dtoList = this.insQueryApiService.selectSubInsCompList(qc);
        }else{
            BaseLogUtils.info(logger, "getCompInfoList", "获取保险公司总公司信息");

            dtoList = this.insQueryApiService.selectInsCompList(qc);
        }
        if (cn.cmm.base.framework.utils.CollectionUtils.isNotEmpty(dtoList)) {
            for (InsCompInfoRDTO insCompInfoRDTO : dtoList) {
                KeyValueVO valueVO = new KeyValueVO();
                valueVO.setKey(String.valueOf(insCompInfoRDTO.getCompId()));
                valueVO.setValue(insCompInfoRDTO.getCompNm());
                provinceKVList.add(valueVO);
            }
        }
        return provinceKVList;

    }

    @Override
    public Map getCompCity(Long id) throws BaseException {
        List<InsCompInfoRDTO> dtoList = null;
        Map<String,String> map = new HashMap<>();
        InsCompInfoRQC qc = new InsCompInfoRQC();
        qc.setCompType(InsCompInfoRConstant.INS_COMP_TYPE_FQ);
        qc.setCompState(InsCompInfoRConstant.INS_COMP_STATE_CD);
        qc.setCompId(id);
        dtoList =this.insQueryApiService.selectSubInsCompList(qc);
        if (cn.cmm.base.framework.utils.CollectionUtils.isNotEmpty(dtoList)) {
            InsCompInfoRDTO dto = dtoList.get(0);
            map.put("rgnPrCd",dto.getRgnPrCd());
            map.put("rgnPrNm",dto.getRgnPrNm());
            map.put("rgnCyCd",dto.getRgnCyCd());
            map.put("rgnCyNm",dto.getRgnCyNm());
        }
        return map;
    }
}
